/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.system.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.codec.Charsets;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.sms.service.ISmsService;
import org.springblade.modules.system.dto.UserDTO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.excel.UserExcel;
import org.springblade.modules.system.excel.UserImportListener;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 控制器
 *
 * @author Chill
 */
@ApiIgnore
@RestController
@RequestMapping("/blade-user")
@AllArgsConstructor
public class UserController {

    private IUserService userService;
    private ISmsService smsService;

    /**
     * 查询单条
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/detail")
    public R<UserVO> detail(User user) {
        User detail = userService.getOne(Condition.getQueryWrapper(user));
        return R.data(UserWrapper.build().entityVO(detail));
    }

    /**
     * 查询单条
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/info")
    public R<UserVO> info(BladeUser user) {
        User detail = userService.getById(user.getUserId());
        return R.data(UserWrapper.build().entityVO(detail));
    }

    /**
     * 用户列表
     */
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "列表", notes = "传入account和realName")
    public R<IPage<UserVO>> list(@ApiIgnore @RequestParam Map<String, Object> user, Query query, BladeUser bladeUser) {
        QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
        IPage<User> pages = userService.page(Condition.getPage(query), (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId()) : queryWrapper);
        return R.data(UserWrapper.build().pageVO(pages));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增或修改", notes = "传入User")
    public R submit(@Valid @RequestBody User user) {
        return R.status(userService.submit(user));
    }

    /**
     *
     * 新增或修改
     */
    @PostMapping("/addUser")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增或修改", notes = "传入User")
    public R addUser(@Valid @RequestBody User user) {
        return R.status(userService.addUser(user));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入User")
    public R update(@Valid @RequestBody User user) {
        return R.status(userService.updateById(user));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除", notes = "传入id")
    public R remove(@RequestParam String ids) {
        if (Func.contains(Func.toLongArray(ids), SecureUtil.getUserId())) {
            throw new ServiceException("不能删除本账号!");
        }
        return R.status(userService.deleteLogic(Func.toLongList(ids)));
    }


    /**
     * 设置菜单权限
     *
     * @param userIds
     * @param roleIds
     * @return
     */
    @PostMapping("/grant")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "权限设置", notes = "传入roleId集合以及menuId集合")
    public R grant(@ApiParam(value = "userId集合", required = true) @RequestParam String userIds,
                   @ApiParam(value = "roleId集合", required = true) @RequestParam String roleIds) {
        boolean temp = userService.grant(userIds, roleIds);
        return R.status(temp);
    }

    @PostMapping("/reset-password")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "初始化密码", notes = "传入userId集合")
    public R resetPassword(@ApiParam(value = "userId集合", required = true) @RequestParam String userIds) {
        boolean temp = userService.resetPassword(userIds);
        return R.status(temp);
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param newPassword
     * @param newPassword1
     * @return
     */
    @PostMapping("/update-password")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "修改密码", notes = "传入密码")
    public R updatePassword(BladeUser user, @ApiParam(value = "旧密码", required = true) @RequestParam String oldPassword,
                            @ApiParam(value = "新密码", required = true) @RequestParam String newPassword,
                            @ApiParam(value = "新密码", required = true) @RequestParam String newPassword1) {
        boolean temp = userService.updatePassword(user.getUserId(), oldPassword, newPassword, newPassword1);
        return R.status(temp);
    }

    //前台修改密码
    @PostMapping("/updatePassword")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "修改密码", notes = "传入密码")
    public R updatePasswords(@RequestBody UserDTO user) {
        User byId = userService.getById(user.getId());
        if (!byId.getPassword().equals(DigestUtil.encrypt(user.getOldPassword()))) {
            throw new ServiceException("原密码不正确!");
        }
        byId.setPassword(DigestUtil.encrypt(user.getNewPassword()));
        return R.status(userService.updateById(byId));
    }

    /**
     * 用户列表
     *
     * @param user
     * @return
     */
    @GetMapping("/user-list")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "用户列表", notes = "传入user")
    public R<List<User>> userList(User user) {
        List<User> list = userService.list(Condition.getQueryWrapper(user));
        return R.data(list);
    }


    /**
     * 导入用户
     */
    @PostMapping("import-user")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "导入用户", notes = "传入excel")
    public R importUser(MultipartFile file, Integer isCovered) {
        String filename = file.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            throw new RuntimeException("请上传文件!");
        }
        if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
            throw new RuntimeException("请上传正确的excel文件!");
        }
        InputStream inputStream;
        try {
            UserImportListener importListener = new UserImportListener(userService);
            inputStream = new BufferedInputStream(file.getInputStream());
            ExcelReaderBuilder builder = EasyExcel.read(inputStream, UserExcel.class, importListener);
            builder.doReadAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.success("操作成功");
    }

    /**
     * 导出用户
     */
    @SneakyThrows
    @GetMapping("export-user")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "导出用户", notes = "传入user")
    public void exportUser(@ApiIgnore @RequestParam Map<String, Object> user, BladeUser bladeUser, HttpServletResponse response) {
        QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
        if (!SecureUtil.isAdministrator()) {
            queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId());
        }
        queryWrapper.lambda().eq(User::getIsDeleted, BladeConstant.DB_NOT_DELETED);
        List<UserExcel> list = userService.exportUser(queryWrapper);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(Charsets.UTF_8.name());
        String fileName = URLEncoder.encode("用户数据导出", Charsets.UTF_8.name());
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserExcel.class).sheet("用户数据表").doWrite(list);
    }

    /**
     * 导出模板
     */
    @SneakyThrows
    @GetMapping("export-template")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "导出模板")
    public void exportUser(HttpServletResponse response) {
        List<UserExcel> list = new ArrayList<>();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(Charsets.UTF_8.name());
        String fileName = URLEncoder.encode("用户数据模板", Charsets.UTF_8.name());
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserExcel.class).sheet("用户数据表").doWrite(list);
    }

    /**
     * 第三方注册用户
     */
    @PostMapping("/register-guest")
    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "第三方注册用户", notes = "传入user")
    public R registerGuest(User user, Long oauthId) {
        return R.status(userService.registerGuest(user, oauthId));
    }

    /**
     * 用户信息
     */
    @GetMapping("/user")
    @ApiOperationSupport(order = 16)
    @ApiOperation(value = "用户信息", notes = "")
    public R user() {
        User user = userService.getById(SecureUtil.getUser().getUserId());
        UserVO userVO = BeanUtil.copy(user, UserVO.class);
        userVO.setRoleName(SecureUtil.getUser().getRoleName());
        return R.data(userVO);
    }

    /**
     * 根据用户账号、手机号查询用户
     */
    @GetMapping("/getUserMessage")
    @ApiOperationSupport(order = 17)
    @ApiOperation(value = "用户信息", notes = "")
    public R getUserCount(String account, String phone) {
        User user = userService.getOne(Wrappers.<User>query().lambda()
                .eq(User::getAccount, account).eq(User::getPhone, phone));
        return R.data(user);
    }

    /**
     * 根据账号、手机号修改密码
     */
    @PostMapping("/getUpdatePassword")
    @ApiOperationSupport(order = 18)
    @ApiOperation(value = "忘记密码", notes = "")
    public R getUpdatePassword(@RequestBody UserDTO userDTO) {
        boolean smsMessage = smsService.smsMessage(userDTO.getPhone(), userDTO.getPhoneCode());
        if (!smsMessage) {
            throw new ApiException("验证码错误！");
        }
        userDTO.setPassword(DigestUtil.sha(userDTO.getPassword()));
        boolean update = userService.updateById(userDTO);
        return R.data(update);
    }


    @PostMapping("/upload")
    @ApiOperationSupport(order = 19)
    @ApiOperation(value = "上传图片", notes = "")
    public R get(MultipartFile file) {
        try {
            String filenames = file.getOriginalFilename();
            String filename = UUID.randomUUID().toString();
            //String path = "D:\\zzz\\saber\\public\\img";
            String path ="E:\\project\\Saber\\public\\img";
            String name = filenames.substring(filenames.lastIndexOf("."));
            //获取输入流
            InputStream is = file.getInputStream();
            //获取输出流
            File fileImg = new File(path + "/" + filename + name);
            FileOutputStream os = new FileOutputStream(fileImg);
            int i = 0;
            while ((i = is.read()) != -1) {
                os.write(i);
            }
            os.close();
            is.close();
            User user = userService.getById(SecureUtil.getUser().getUserId());
            user.setAvatar("img/" + filename + name);
            userService.updateById(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.status(true);
    }

    /**
     * 前台登录
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/login")
    public R<User> login(User user) {
        user.setPassword(DigestUtil.sha(user.getPassword()));
        User detail = userService.getOne(Condition.getQueryWrapper(user));
        if (Func.isNotEmpty(detail)){
            if (Func.isEmpty(detail.getAvatar())){
                detail.setAvatar(CommonConstant.AVATAR_IMG);
            }
        }
        return R.data(detail);
    }
}
